/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.web;

import org.xptracker.domain.Domain;
import org.xptracker.domain.SavedQuery;
import org.xptracker.domain.Story;
import org.xptracker.persistence.PersistenceManager;
import org.xptracker.search.Searcher;

import java.util.List;
import java.util.SortedMap;

public class SavedQuerySource implements StorySource {
  private static final String ID_PREFIX = "sq_";
  private transient Searcher searcher;
  private String name;
  private String id;

  public SavedQuerySource(SavedQuery query, Searcher searcher) {
    this.searcher = searcher;
    this.name = query.getName();
    this.id = ID_PREFIX + query.getId();
  }

  public List<Story> getStories(Domain context) {
    return searcher.search(Story.class, loadQuery(context).getQuery());
  }

  private SavedQuery loadQuery(Domain context) {
    return context.loadEntity(SavedQuery.class, getActualId(id));
  }

  public String getId() {
    return id;
  }

  public String getName() {
    return name;
  }

  public boolean isReorderable() {
    return false;
  }

  public boolean isAlive(Domain context) {
    //If this source gets serialized, we can no longer perform searches.
    return searcher != null && loadQuery(context) != null;
  }

  public void addStory(Domain domain, PersistenceManager.UpdateReceiver receiver, QueueNotifier queueNotifier, Story story) {
    throw new UnsupportedOperationException("Can't add to this source.");
  }

  public void changeOrder(Domain domain, PersistenceManager.UpdateReceiver receiver, SortedMap<Integer,Story> reorderings) {
    throw new UnsupportedOperationException("Can't reorder this source.");
  }

  public String getSourceType() {
    return "Saved Searches";
  }

  public static boolean isSavedQuerySource(String id) {
    return id.startsWith(ID_PREFIX);
  }

  public static StorySource newInstance(String id, Domain domain, Searcher searcher) {
    SavedQuery query = domain.loadEntity(SavedQuery.class, getActualId(id));
    return query == null ? null : new SavedQuerySource(query, searcher);
  }

  private static String getActualId(String id) {
    return id.substring(ID_PREFIX.length());
  }

  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    SavedQuerySource that = (SavedQuerySource) o;

    if (id != null ? !id.equals(that.id) : that.id != null) return false;

    return true;
  }

  public int hashCode() {
    return (id != null ? id.hashCode() : 0);
  }
}
